package edu.seu.mart.service.impl;

import edu.seu.mart.dao.IAdminDao;
import edu.seu.mart.model.Admin;
import edu.seu.mart.model.MartException;
import edu.seu.mart.service.IAdminService;
import edu.seu.mart.utils.SecurityUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.security.NoSuchAlgorithmException;

/**
 * Created by samsung on 2015/3/6.
 */
@Service
public class AdminService implements IAdminService {

    @Resource
    private IAdminDao adminDao;

    @Override
    public Admin login(String username, String password) {
        Admin admin = adminDao.loadByUsername(username);
        if (admin == null) throw new MartException("用户名或者密码不正确");
        try {
            if (!SecurityUtil.md5(username, password).equals(admin.getPassword())) {
                throw new MartException("用户名或者密码不正确");
            }
        } catch (NoSuchAlgorithmException e) {
            throw new MartException("密码加密失败:" + e.getMessage());
        }
        return admin;
    }
}
